//
//  LYHotVideoViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/11.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYHotVideoViewController.h"
#import "LYSearchBar.h"
#import "LYVideoModel.h"
#import "LYMineVideoCell.h"
#import "LYCollectListModel.h"
#import "LYVideoWebViewController.h"
@interface LYHotVideoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray *models;

@property (assign, nonatomic) NSInteger page;

@property (nonatomic, strong) LYSearchBar *searchBar;


@end

@implementation LYHotVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = self.searchBar;
    
    [self.tableView.mj_header beginRefreshing];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 懒加载
- (LYSearchBar *)searchBar{
    
    if (_searchBar == nil) {
        LYSearchBar *searchBar = [[LYSearchBar alloc] init];
        searchBar.frame = CGRectMake(0, 0, CGFLOAT_MAX, 30);
        searchBar.placeholder = @"搜索科目、老师、课程、机构";
        [searchBar addTarget:self action:@selector(textfieldChange:) forControlEvents:UIControlEventEditingChanged];
        
        _searchBar = searchBar;
    }
    return _searchBar;
}
- (void)textfieldChange:(UITextField *)searchBar{
    
    
    [self loadNewData];
    
    
}


#pragma mark - 网络请求

/**
 *  请求新数据
 */
- (void)loadNewData{
    
    [LYRequestTool getVideoListWithToken:LYToken to_user_id:nil keyword:self.searchBar.text order:nil page:@"1" per_page:@"10" success:^(NSArray<LYMyVideoListModel *> *models) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer resetNoMoreData];
        self.models = [models mutableCopy];
        [self.tableView reloadData];

        
    } failure:^(NSString *code, NSString *message) {
        [self.tableView.mj_header endRefreshing];
    }];
    

    
}
/**
 *  加载更多数据
 */
- (void)loadMoreData{
    
    NSString *page = [NSString stringWithFormat:@"%zd",self.page + 1];
    
    [LYRequestTool getVideoListWithToken:LYToken to_user_id:nil keyword:self.searchBar.text order:nil page:page per_page:@"10" success:^(NSArray<LYVideoListModel *> *models) {
        
        if (models.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
        
        [self.tableView.mj_footer endRefreshing];
        
        [self.models addObjectsFromArray:models];;
        self.page++;
        [self.tableView reloadData];
        
        
        
    } failure:^(NSString *code, NSString *message) {
         [self.tableView.mj_footer endRefreshing];

    }];


}

#pragma mark - 上下拉刷新
/**
 *  下拉
 */
- (void)tableViewHeaderRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadNewData];
    
}

/**
 *  上拉
 */
- (void)tableViewfooterRefreshMethod:(VVYRefreshTableView *)tableView{
    
    [self loadMoreData];
    
}









#pragma mark - tableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    LYMyVideoListModel *model = self.models[indexPath.row];
    LYVideoWebViewController *videoVC = [LYVideoWebViewController videoWebVCWithID:model.Id];
    [self.navigationController pushViewController:videoVC animated:YES];

}

#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LYMineVideoCell";
    LYMineVideoCell *cell = [LYMineVideoCell cellWithTableView:tableView andIndetifier:cellID];
    
    //model
    LYMyVideoListModel *model = self.models[indexPath.row];
    cell.model = model;
    
    return cell;
    
}


@end
